from rest_framework import serializers
from django.contrib.auth import get_user_model
from .models import *

# User = get_user_model()


class UserCommentsSerializer(serializers.ModelSerializer):

    user = serializers.HiddenField(
        default=serializers.CurrentUserDefault()
    )
    # add_time = serializers.DateTimeField(read_only=True)

    def create(self, validated_data):
        user = self.context['request']._user

        data = user.has_perm('user_operation.cannot_add_comments')

        if data:
            raise serializers.ValidationError("你没有权限发帖,请联系管理员")

        return super().create(validated_data)

    # def update(self, instance, validated_data):
    #     permissions = get_perms(self.context['request']._user, instance)
    #     if 'delete_usercomments' not in permissions:
    #         raise serializers.ValidationError('你没有权限去删除')
    #     return super().perform_destroy(instance)
    #     user = self.context['request']._user
    #
    #     data = user.has_perm('user_operation.')
    #
    #     if data:
    #         raise serializers.ValidationError("你没有权限发帖,请联系管理员")
    #
    #     return super().update(validated_data)

    class Meta:
        model = UserComments
        fields = '__all__'

    def is_valid(self, raise_exception=False):
        s = 1
        return super().is_valid(raise_exception=False)

# class UserCreatePlaylistSerializer(serializers.ModelSerializer):
#     user = serializers.HiddenField(
#         default=serializers.CurrentUserDefault()
#     )
#
#     add_time = serializers.DateTimeField(read_only=True)
#
#     class Meta:
#         model = UserCreatePlaylist
#         fields = ('user', 'playlist', 'song', 'add_time',)